import Vue from 'vue'
import Router from 'vue-router'
// 导入login组件
import Login from './components/login.vue'
import Home from './components/home.vue'
import Welcome from './components/welcome.vue'
import Users from './components/users.vue'
import './assets/css/global.css'
// power代码部分
import Rigths from './components/power/rights.vue'
import Roles from './components/power/roles.vue'
// goods代码部分
import Categories from './components/goods/categories.vue'
import Params from './components/goods/params.vue'
import Goods from './components/goods/goods.vue'
import Add from './components/goods/add.vue'
// order 代码部分
import Order from './components/order/order.vue'
import Report from './components/report/report.vue'
Vue.use(Router)

const router = new Router({
  routes: [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    redirect: '/welcome',
    component: Home,
    children: [{
      path: '/welcome',
      component: Welcome
    },
    {
      path: '/users',
      component: Users
    },
    {
      path: '/rights',
      component: Rigths
    },
    {
      path: '/roles',
      component: Roles
    },
    {
      path: '/categories',
      component: Categories
    },
    {
      path: '/params',
      component: Params
    },
    {
      path: '/goods',
      component: Goods
    },
    {
      path: '/goods/add',
      component: Add
    },
    {
      path: '/orders',
      component: Order
    },
    {
      path: '/reports',
      component: Report
    }]
  }
  ]
})
// 挂载路由守卫
router.beforeEach((to, from, next) => {
  // to and from are both route objects. must call `next`.
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
